package com.lp.module_lib.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.lp.module_lib.R;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.network.mvp.BaseModel;
import com.lp.module_lib.network.mvp.BaseView;

import butterknife.ButterKnife;

/**
 * 作者：created by LP_TREAM on 2020/11/26 19:52
 * 邮箱：806875201@qq.com
 */
public abstract class BaseFragment<P extends BasePresenter> extends SupportFragment implements BaseView {
    protected P mPresenter;

    protected abstract P createPresenter();

    protected View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(getLayoutId(), container, false);

        ARouter.getInstance().inject(this);

        ButterKnife.bind(this, rootView);

        mContext = getActivity();
        mPresenter = createPresenter();

        this.initData();

        this.initRefreshLayout(setRefreshLayout());

        return rootView;
    }

    @Override
    public void onErrorState(BaseModel model, int mType) {
        showToast(model.getMsg());
        if (mSmoothRefreshLayout != null) {
            mSmoothRefreshLayout.refreshComplete();
        }
    }

    /**
     * 获取布局ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 数据初始化操作
     */
    protected abstract void initData();

    @Override
    public void showLoading(Boolean isShowProgress) {
        if (isShowProgress){
            showProgressDialog();
        }else {
            showLoadingDialog();
        }
    }

    @Override
    public void hideLoading() {
        dissMissDialog();
        hideProgressDialog();
    }

    public void showLoadingDialog() {
        showLoadingDialog(getString(R.string.app_loading));
    }

    @Override
    public void onProgress(int progress) {
        onProgressDialog(progress);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.rootView = null;
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.bind(getActivity()).unbind();
    }

}
